//
//  HomeViewCell.swift
//  KKWB
//
//  Created by KSY-iOS on 17/1/17.
//  Copyright © 2017年 KK. All rights reserved.
//

import UIKit
import SDWebImage
import HYLabel

private let edgeMargin: CGFloat = 15
private let itemMargin: CGFloat = 10

class HomeViewCell: UITableViewCell {

    // MARK:- 属性
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var verifedView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: HYLabel!
    @IBOutlet weak var picCollectionView: PicCollectionView!
    @IBOutlet weak var retweetedContentLabel: HYLabel!
    @IBOutlet weak var retweetedBgView: UIView!
    @IBOutlet weak var bottomView: UIView!
    
    // MARK:- 约束属性
    @IBOutlet weak var picViewHeightConstarint: NSLayoutConstraint!
    @IBOutlet weak var picViewWidthConstarint: NSLayoutConstraint!
    
    
    var viewModel: StatusViewModel? {
        didSet {
            guard let viewModel = viewModel else {
                return
            }
            
            iconView.sd_setImage(with: viewModel.profileURL, placeholderImage: UIImage(named: "avatar_default_"))
            verifedView.image = viewModel.verified_image
            screenNameLabel.text = viewModel.status?.user?.screen_name
            vipView.image = viewModel.vip_image
            timeLabel.text = viewModel.creatAtText
            sourceLabel.text = viewModel.sourceText
            contentLabel.text = viewModel.status?.text
            
            screenNameLabel.textColor = viewModel.vip_image == nil ? UIColor.black : UIColor.orange
            
            // 计算配图的view 约束
            let picViewSize = calculatePicViewSize(count: viewModel.picURLs.count)
            
            picViewWidthConstarint.constant = picViewSize.width
            picViewHeightConstarint.constant = picViewSize.height
        
            // 将pictureurl 数据传输给picview
            picCollectionView.picUrls = viewModel.picURLs
            
            // 设置转发微博的正文
            if  viewModel.status?.retweeted_status != nil,
                let screenName = viewModel.status?.retweeted_status?.user?.screen_name,
                let retweetedText = viewModel.status?.retweeted_status?.text
            {
                retweetedContentLabel.text = "@" + "\(screenName): " + "\(retweetedText)"
                retweetedBgView.isHidden = false
            } else {
                retweetedContentLabel.text = nil
                retweetedBgView.isHidden = true
            }
            
            // 获取 cell 的高度
            if viewModel.cellHeight == 0 {
                layoutIfNeeded()
                let maxY = bottomView.frame.maxY
                viewModel.cellHeight = maxY
            }
        }
    }
    
    
    @IBOutlet weak var contentLabelWidthConstraint: NSLayoutConstraint!
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentLabelWidthConstraint.constant = UIScreen.main.bounds.width - 2 * edgeMargin
        
        // 设置单元格大小
        let layout = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        let imageViewH = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * itemMargin) / 3
        layout.itemSize = CGSize(width: imageViewH, height: imageViewH)
        
        // 设置HYLabel的内容
        contentLabel.matchTextColor = UIColor.purple
        retweetedContentLabel.matchTextColor = UIColor.purple
        
        // 监听HYlabel内容的点击
        // 监听@谁谁谁的点击
        contentLabel.userTapHandler = { (label, user, range) in
            print(user)
            print(range)
        }
        
        // 监听链接的点击
        contentLabel.linkTapHandler = { (label, link, range) in
            print(link)
            print(range)
        }
        
        // 监听话题的点击
        contentLabel.topicTapHandler = { (label, topic, range) in
            print(topic)
            print(range)
        }
        
    }
}

extension HomeViewCell {
    
    fileprivate func calculatePicViewSize(count: Int) -> CGSize {
        
        if count == 0 {
            return CGSize(width: 0, height: 0)
        }
        
        let imageViewH = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * itemMargin) / 3
        
        if count == 4 {
            
            let picViewWH = imageViewH * 2 + itemMargin + 1
            return CGSize(width: picViewWH, height: picViewWH)
        }
        
        let rows = CGFloat((count - 1) / 3 + 1)
        let picViewH = rows * imageViewH + (rows - 1) * itemMargin
        let picViewW = UIScreen.main.bounds.width - 2 * edgeMargin
        
        return CGSize(width: picViewW, height: picViewH)
    }
}







